package com.ww.springboot.boot.thread.study.base.interrupt;

import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.LockSupport;

/**
 * @author wanwei
 * @since 2021-04-17 15:46
 */
public class InterruptDemo3 {

    public static void main(String[] args) throws InterruptedException {
        Thread thread = new Thread() {
            @Override
            public void run() {
                while (true) {
                    //do nothing, just empty loop.
                    try {
                        TimeUnit.SECONDS.sleep(1);
                    } catch (InterruptedException e) {
                        System.out.println(e);
                    }
                }
            }
        };
        //设置为守护线程-》线程结束 jvm关闭 只能在线程未启动前设置
        thread.setDaemon(true);
        thread.start();
        TimeUnit.MILLISECONDS.sleep(2);
        System.out.printf("Thread is interrupted ? %s\n", thread.isInterrupted());
        thread.interrupt();
        System.out.printf("Thread is interrupted ? %s\n", thread.isInterrupted());
        Thread.sleep(2000);
        LockSupport.unpark(thread);
    }
}
